//==========================================================================================
//
//		System.Windows.Forms.AxHost
//		Copyright (c) 2005, OpenNETCF.org
//
//		This library is free software; you can redistribute it and/or modify it under 
//		the terms of the OpenNETCF.org Shared Source License.
//
//		This library is distributed in the hope that it will be useful, but 
//		WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
//		FITNESS FOR A PARTICULAR PURPOSE. See the OpenNETCF.org Shared Source License 
//		for more details.
//
//		You should have received a copy of the OpenNETCF.org Shared Source License 
//		along with this library; if not, email licensing@opennetcf.org to request a copy.
//
//		If you wish to contact the OpenNETCF Advisory Board to discuss licensing, please 
//		email licensing@opennetcf.org.
//
//		For general enquiries, email enquiries@opennetcf.org or visit our website at:
//		http://www.opennetcf.org
//
//==========================================================================================

using System;
using System.Runtime.InteropServices.ComTypes;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;

namespace System.Windows.Forms
{

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000118-0000-0000-C000-000000000046")]
    public interface IOleClientSite
    {
        [PreserveSig]
        int SaveObject();
        [PreserveSig]
        int GetMoniker([MarshalAs(UnmanagedType.U4)] int dwAssign, [In, MarshalAs(UnmanagedType.U4)] int dwWhichMoniker, [MarshalAs(UnmanagedType.Interface)] out object moniker);
        [PreserveSig]
        int GetContainer(out IOleContainer container);
        [PreserveSig]
        int ShowObject();
        [PreserveSig]
        int OnShowWindow(int fShow);
        [PreserveSig]
        int RequestNewObjectLayout();
    }


    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("B196B289-BAB4-101A-B69C-00AA00341D07")]
    public interface IOleControlSite
    {
        [PreserveSig]
        int OnControlInfoChanged();
        [PreserveSig]
        int LockInPlaceActive(int fLock);
        [PreserveSig]
        int GetExtendedControl([MarshalAs(UnmanagedType.IDispatch)] out object ppDisp);
        [PreserveSig]
        int TransformCoords([In, Out] POINTL pPtlHimetric, [In, Out] tagPOINTF pPtfContainer, [In, MarshalAs(UnmanagedType.U4)] int dwFlags);
        [PreserveSig]
        int TranslateAccelerator([In] ref MSG pMsg, [In, MarshalAs(UnmanagedType.U4)] int grfModifiers);
        [PreserveSig]
        int OnFocus(int fGotFocus);
        [PreserveSig]
        int ShowPropertyFrame();
    }

    [ComImport, Guid("00000119-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleInPlaceSite
    {
        IntPtr GetWindow();
        [PreserveSig]
        int ContextSensitiveHelp(int fEnterMode);
        [PreserveSig]
        int CanInPlaceActivate();
        [PreserveSig]
        int OnInPlaceActivate();
        [PreserveSig]
        int OnUIActivate();
        [PreserveSig]
        int GetWindowContext([MarshalAs(UnmanagedType.Interface)] out IOleInPlaceFrame ppFrame, [MarshalAs(UnmanagedType.Interface)] out IOleInPlaceUIWindow ppDoc, [Out] COMRECT lprcPosRect, [Out] COMRECT lprcClipRect, [In, Out] tagOIFI lpFrameInfo);
        [PreserveSig]
        int Scroll(tagSIZE scrollExtant);
        [PreserveSig]
        int OnUIDeactivate(int fUndoable);
        [PreserveSig]
        int OnInPlaceDeactivate();
        [PreserveSig]
        int DiscardUndoState();
        [PreserveSig]
        int DeactivateAndUndo();
        [PreserveSig]
        int OnPosRectChange([In] COMRECT lprcPosRect);
    }

    [ComImport, Guid("9BFBBC02-EFF1-101A-84ED-00AA00341D07"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IPropertyNotifySink
    {
        void OnChanged(int dispID);
        [PreserveSig]
        int OnRequestEdit(int dispID);
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("0000011B-0000-0000-C000-000000000046")]
    public interface IOleContainer
    {
        [PreserveSig]
        int ParseDisplayName([In, MarshalAs(UnmanagedType.Interface)] object pbc, [In, MarshalAs(UnmanagedType.BStr)] string pszDisplayName, [Out, MarshalAs(UnmanagedType.LPArray)] int[] pchEaten, [Out, MarshalAs(UnmanagedType.LPArray)] object[] ppmkOut);
        [PreserveSig]
        int EnumObjects([In, MarshalAs(UnmanagedType.U4)] int grfFlags, out IEnumUnknown ppenum);
        [PreserveSig]
        int LockContainer(bool fLock);
    }

    [ComImport, Guid("B196B288-BAB4-101A-B69C-00AA00341D07"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleControl
    {
        [PreserveSig]
        int GetControlInfo([Out] tagCONTROLINFO pCI);
        [PreserveSig]
        int OnMnemonic([In] ref MSG pMsg);
        [PreserveSig]
        int OnAmbientPropertyChange(int dispID);
        [PreserveSig]
        int FreezeEvents(int bFreeze);
    }

    [ComImport, Guid("00000113-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleInPlaceObject
    {
        [PreserveSig]
        int GetWindow(out IntPtr hwnd);
        void ContextSensitiveHelp(int fEnterMode);
        void InPlaceDeactivate();
        [PreserveSig]
        int UIDeactivate();
        void SetObjectRects(COMRECT lprcPosRect, COMRECT lprcClipRect);
        void ReactivateAndUndo();
    }


    [ComImport, Guid("00000112-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleObject
    {
        [PreserveSig]
        int SetClientSite([In, MarshalAs(UnmanagedType.Interface)] IOleClientSite pClientSite);
        IOleClientSite GetClientSite();
        [PreserveSig]
        int SetHostNames([In, MarshalAs(UnmanagedType.LPWStr)] string szContainerApp, [In, MarshalAs(UnmanagedType.LPWStr)] string szContainerObj);
        [PreserveSig]
        int Close(int dwSaveOption);
        [PreserveSig]
        int SetMoniker([In, MarshalAs(UnmanagedType.U4)] int dwWhichMoniker, [In, MarshalAs(UnmanagedType.Interface)] object pmk);
        [PreserveSig]
        int GetMoniker([In, MarshalAs(UnmanagedType.U4)] int dwAssign, [In, MarshalAs(UnmanagedType.U4)] int dwWhichMoniker, [MarshalAs(UnmanagedType.Interface)] out object moniker);
        [PreserveSig]
        int InitFromData([In, MarshalAs(UnmanagedType.Interface)] IDataObject pDataObject, int fCreation, [In, MarshalAs(UnmanagedType.U4)] int dwReserved);
        [PreserveSig]
        int GetClipboardData([In, MarshalAs(UnmanagedType.U4)] int dwReserved, out IDataObject data);
        [PreserveSig]
        int DoVerb(int iVerb, [In] IntPtr lpmsg, [In, MarshalAs(UnmanagedType.Interface)] IOleClientSite pActiveSite, int lindex, IntPtr hwndParent, [In] COMRECT lprcPosRect);
        [PreserveSig]
        int EnumVerbs(out IEnumOLEVERB e);
        [PreserveSig]
        int OleUpdate();
        [PreserveSig]
        int IsUpToDate();
        [PreserveSig]
        int GetUserClassID([In, Out] ref Guid pClsid);
        [PreserveSig]
        int GetUserType([In, MarshalAs(UnmanagedType.U4)] int dwFormOfType, [MarshalAs(UnmanagedType.LPWStr)] out string userType);
        [PreserveSig]
        int SetExtent([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, [In] tagSIZEL pSizel);
        [PreserveSig]
        int GetExtent([In, MarshalAs(UnmanagedType.U4)] int dwDrawAspect, [Out] tagSIZEL pSizel);
        [PreserveSig]
        int Advise(IAdviseSink pAdvSink, out int cookie);
        [PreserveSig]
        int Unadvise([In, MarshalAs(UnmanagedType.U4)] int dwConnection);
        [PreserveSig]
        int EnumAdvise(out IEnumSTATDATA e);
        [PreserveSig]
        int GetMiscStatus([In, MarshalAs(UnmanagedType.U4)] int dwAspect, out int misc);
        [PreserveSig]
        int SetColorScheme([In] /*tagLOGPALETTE*/IntPtr pLogpal);
    }

    [StructLayout(LayoutKind.Sequential)]
    public sealed class tagQACONTAINER
    {
        [MarshalAs(UnmanagedType.U4)]
        public int cbSize;
        public /*IOleClientSite*/IntPtr pClientSite;
        //[MarshalAs(UnmanagedType.Interface)]
        public IntPtr pAdviseSink;
        public /*IPropertyNotifySink*/IntPtr pPropertyNotifySink;
        //[MarshalAs(UnmanagedType.Interface)]
        public IntPtr pUnkEventSink;
        [MarshalAs(UnmanagedType.U4)]
        public int dwAmbientFlags;
        [MarshalAs(UnmanagedType.U4)]
        public uint colorFore;
        [MarshalAs(UnmanagedType.U4)]
        public uint colorBack;
        //[MarshalAs(UnmanagedType.Interface)]
        public IntPtr pFont;
        //[MarshalAs(UnmanagedType.Interface)]
        public IntPtr pUndoMgr;
        [MarshalAs(UnmanagedType.U4)]
        public int dwAppearance;
        public int lcid;
        public IntPtr hpal;
        //[MarshalAs(UnmanagedType.Interface)]
        public IntPtr pBindHost;
        public tagQACONTAINER() { cbSize = Marshal.SizeOf(typeof(tagQACONTAINER)); }
    }

    [StructLayout(LayoutKind.Sequential)]
    public sealed class tagQACONTROL
    {
        [MarshalAs(UnmanagedType.U4)]
        public int cbSize;
        [MarshalAs(UnmanagedType.U4)]
        public int dwMiscStatus;
        [MarshalAs(UnmanagedType.U4)]
        public int dwViewStatus;
        [MarshalAs(UnmanagedType.U4)]
        public int dwEventCookie;
        [MarshalAs(UnmanagedType.U4)]
        public int dwPropNotifyCookie;
        [MarshalAs(UnmanagedType.U4)]
        public int dwPointerActivationPolicy;
        public tagQACONTROL() { cbSize = Marshal.SizeOf(typeof(tagQACONTROL)); }
    }

    [StructLayout(LayoutKind.Sequential)]
    public class COMRECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
        public COMRECT() { }
        public COMRECT(Rectangle r)
        {
            this.left = r.X;
            this.top = r.Y;
            this.right = r.Right;
            this.bottom = r.Bottom;
        }
 

        public COMRECT(int left, int top, int right, int bottom)
        {
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
        }
        public static COMRECT FromXYWH(int x, int y, int width, int height)
        {
            return new COMRECT(x, y, x + width, y + height);
        }

        public override string ToString()
        {
            return string.Concat(new object[8] { "Left = ", this.left, " Top ", this.top, " Right = ", this.right, " Bottom = ", this.bottom });
        }
 

    }

    [StructLayout(LayoutKind.Sequential)]
    public class OLECMD
    {
        [MarshalAs(UnmanagedType.U4)]
        public int cmdID;
        [MarshalAs(UnmanagedType.U4)]
        public int cmdf;
    }

    [ComImport, Guid("00000116-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleInPlaceFrame
    {
        IntPtr GetWindow();
        [PreserveSig]
        int ContextSensitiveHelp(int fEnterMode);
        [PreserveSig]
        int GetBorder([Out] COMRECT lprectBorder);
        [PreserveSig]
        int RequestBorderSpace([In] COMRECT pborderwidths);
        [PreserveSig]
        int SetBorderSpace([In] COMRECT pborderwidths);
        [PreserveSig]
        int SetActiveObject([In, MarshalAs(UnmanagedType.Interface)] IOleInPlaceActiveObject pActiveObject, [In, MarshalAs(UnmanagedType.LPWStr)] string pszObjName);
        [PreserveSig]
        int InsertMenus([In] IntPtr hmenuShared, [In, Out] /*tagOleMenuGroupWidths*/IntPtr lpMenuWidths);
        [PreserveSig]
        int SetMenu([In] IntPtr hmenuShared, [In] IntPtr holemenu, [In] IntPtr hwndActiveObject);
        [PreserveSig]
        int RemoveMenus([In] IntPtr hmenuShared);
        [PreserveSig]
        int SetStatusText([In, MarshalAs(UnmanagedType.LPWStr)] string pszStatusText);
        [PreserveSig]
        int EnableModeless(bool fEnable);
        [PreserveSig]
        int TranslateAccelerator([In] ref MSG lpmsg, [In, MarshalAs(UnmanagedType.U2)] short wID);
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000115-0000-0000-C000-000000000046")]
    public interface IOleInPlaceUIWindow
    {
        IntPtr GetWindow();
        [PreserveSig]
        int ContextSensitiveHelp(int fEnterMode);
        [PreserveSig]
        int GetBorder([Out] COMRECT lprectBorder);
        [PreserveSig]
        int RequestBorderSpace([In] COMRECT pborderwidths);
        [PreserveSig]
        int SetBorderSpace([In] COMRECT pborderwidths);
        void SetActiveObject([In, MarshalAs(UnmanagedType.Interface)] IOleInPlaceActiveObject pActiveObject, [In, MarshalAs(UnmanagedType.LPWStr)] string pszObjName);
    }

    /*
    [ComVisible(true)]
    public interface IDataObject
    {
        // Methods
        object GetData(string format);
        object GetData(Type format);
        object GetData(string format, bool autoConvert);
        bool GetDataPresent(string format);
        bool GetDataPresent(Type format);
        bool GetDataPresent(string format, bool autoConvert);
        string[] GetFormats();
        string[] GetFormats(bool autoConvert);
        void SetData(object data);
        void SetData(string format, object data);
        void SetData(Type format, object data);
        void SetData(string format, bool autoConvert, object data);
    }
     */

    [ComImport, Guid("00000104-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IEnumOLEVERB
    {
        [PreserveSig]
        int Next([MarshalAs(UnmanagedType.U4)] int celt, [Out] tagOLEVERB rgelt, [Out, MarshalAs(UnmanagedType.LPArray)] int[] pceltFetched);
        [PreserveSig]
        int Skip([In, MarshalAs(UnmanagedType.U4)] int celt);
        void Reset();
        void Clone(out IEnumOLEVERB ppenum);
    }

    [ComImport, Guid("00000100-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IEnumUnknown
    {
        [PreserveSig]
        int Next([In, MarshalAs(UnmanagedType.U4)] int celt, [Out] IntPtr rgelt, IntPtr pceltFetched);
        [PreserveSig]
        int Skip([In, MarshalAs(UnmanagedType.U4)] int celt);
        void Reset();
        void Clone(out IEnumUnknown ppenum);
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("0000010F-0000-0000-C000-000000000046")]
    public interface IAdviseSink
    {
        [PreserveSig]
        void OnDataChange([In] ref FORMATETC format, [In] ref STGMEDIUM stgmedium);
        [PreserveSig]
        void OnViewChange(int aspect, int index);
        [PreserveSig]
        void OnRename(/*IMoniker*/IntPtr moniker);
        [PreserveSig]
        void OnSave();
        [PreserveSig]
        void OnClose();
    }


    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000103-0000-0000-C000-000000000046")]
    public interface IEnumSTATDATA
    {
        [PreserveSig]
        int Next(int celt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] STATDATA[] rgelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0, SizeConst = 1)] int[] pceltFetched);
        [PreserveSig]
        int Skip(int celt);
        [PreserveSig]
        int Reset();
        void Clone(out IEnumSTATDATA newEnum);
    }

    [ComImport, Guid("00000117-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleInPlaceActiveObject
    {
        [PreserveSig]
        int GetWindow(out IntPtr hwnd);
        void ContextSensitiveHelp(int fEnterMode);
        [PreserveSig]
        int TranslateAccelerator([In] ref MSG lpmsg);
        void OnFrameWindowActivate(int fActivate);
        void OnDocWindowActivate(int fActivate);
        void ResizeBorder(COMRECT prcBorder, [In] IOleInPlaceUIWindow pUIWindow, bool fFrameWindow);
        void EnableModeless(int fEnable);
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("CF51ED10-62FE-11CF-BF86-00A0C9034836")]
    public interface IQuickActivate
    {
        //void QuickActivate(ref tagQACONTAINER pQaContainer, out tagQACONTROL pQaControl);
        void QuickActivate(IntPtr pQaContainer, IntPtr pQaControl);
        void SetContentExtent(ref tagSIZEL pSizel);
        void GetContentExtent(out tagSIZEL pSizel);
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000114-0000-0000-C000-000000000046")]
    public interface IOleWindow
    {
        [PreserveSig]
        int GetWindow(out IntPtr hwnd);
        void ContextSensitiveHelp(int fEnterMode);
    }

    [ComImport, Guid("BEF6E002-A874-101A-8BBA-00AA00300CAB"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IFont
    {
        [return: MarshalAs(UnmanagedType.BStr)]
        string GetName();
        void SetName([MarshalAs(UnmanagedType.BStr)] string pname);
        [return: MarshalAs(UnmanagedType.U8)]
        long GetSize();
        void SetSize([MarshalAs(UnmanagedType.U8)] long psize);
        [return: MarshalAs(UnmanagedType.Bool)]
        bool GetBold();
        void SetBold([MarshalAs(UnmanagedType.Bool)] bool pbold);
        [return: MarshalAs(UnmanagedType.Bool)]
        bool GetItalic();
        void SetItalic([MarshalAs(UnmanagedType.Bool)] bool pitalic);
        [return: MarshalAs(UnmanagedType.Bool)]
        bool GetUnderline();
        void SetUnderline([MarshalAs(UnmanagedType.Bool)] bool punderline);
        [return: MarshalAs(UnmanagedType.Bool)]
        bool GetStrikethrough();
        void SetStrikethrough([MarshalAs(UnmanagedType.Bool)] bool pstrikethrough);
        [return: MarshalAs(UnmanagedType.I2)]
        short GetWeight();
        void SetWeight([MarshalAs(UnmanagedType.I2)] short pweight);
        [return: MarshalAs(UnmanagedType.I2)]
        short GetCharset();
        void SetCharset([MarshalAs(UnmanagedType.I2)] short pcharset);
        IntPtr GetHFont();
        void Clone(out IFont ppfont);
        [PreserveSig]
        int IsEqual([MarshalAs(UnmanagedType.Interface)] IFont pfontOther);
        void SetRatio(int cyLogical, int cyHimetric);
        void QueryTextMetrics(out IntPtr ptm);
        void AddRefHfont(IntPtr hFont);
        void ReleaseHfont(IntPtr hFont);
        void SetHdc(IntPtr hdc);
    }
 
    public sealed class ColorTranslator
    {
        // Methods
        private ColorTranslator() { }
        //public static Color FromHtml(string htmlColor);
        public static Color FromOle(int oleColor)
        {
            if (((oleColor & (-16777216)) == -2147483648) && ((oleColor & 0xffffff) <= 0x18))
            {
                switch (oleColor)
                {
                    case -2147483648:
                        {
                            return (SystemColors.ScrollBar);
                        }
                    case -2147483647:
                        {
                            return (SystemColors.Desktop);
                        }
                    case -2147483646:
                        {
                            return (SystemColors.ActiveCaption);
                        }
                    case -2147483645:
                        {
                            return (SystemColors.InactiveCaption);
                        }
                    case -2147483644:
                        {
                            return (SystemColors.Menu);
                        }
                    case -2147483643:
                        {
                            return (SystemColors.Window);
                        }
                    case -2147483642:
                        {
                            return (SystemColors.WindowFrame);
                        }
                    case -2147483641:
                        {
                            return (SystemColors.MenuText);
                        }
                    case -2147483640:
                        {
                            return (SystemColors.WindowText);
                        }
                    case -2147483639:
                        {
                            return (SystemColors.ActiveCaptionText);
                        }
                    case -2147483638:
                        {
                            return (SystemColors.ActiveBorder);
                        }
                    case -2147483637:
                        {
                            return (SystemColors.InactiveBorder);
                        }
                    case -2147483636:
                        {
                            return (SystemColors.AppWorkspace);
                        }
                    case -2147483635:
                        {
                            return (SystemColors.Highlight);
                        }
                    case -2147483634:
                        {
                            return (SystemColors.HighlightText);
                        }
                    case -2147483633:
                        {
                            return (SystemColors.Control);
                        }
                    case -2147483632:
                        {
                            return (SystemColors.ControlDark);
                        }
                    case -2147483631:
                        {
                            return (SystemColors.GrayText);
                        }
                    case -2147483630:
                        {
                            return (SystemColors.ControlText);
                        }
                    case -2147483629:
                        {
                            return (SystemColors.InactiveCaptionText);
                        }
                    case -2147483628:
                        {
                            return (SystemColors.ControlLightLight);
                        }
                    case -2147483627:
                        {
                            return (SystemColors.ControlDarkDark);
                        }
                    case -2147483626:
                        {
                            return (SystemColors.ControlLight);
                        }
                    case -2147483625:
                        {
                            return (SystemColors.InfoText);
                        }
                    case -2147483624:
                        {
                            return (SystemColors.Info);
                        }
                }
            }
            Color color = Color.FromArgb((byte)(oleColor & 0xff), (byte)((oleColor >> 8) & 0xff), (byte)((oleColor >> 0x10) & 0xff));
            return color;
        }
        public static Color FromWin32(int win32Color)
        {
            return ColorTranslator.FromOle(win32Color);
        }
        //private static void InitializeHtmlSysColorTable();
        //public static string ToHtml(Color c);
        public static int ToOle(Color c)
        {
            if (c.IsSystemColor)
            {
                    if ( c.ToArgb() == SystemColors.ActiveBorder.ToArgb())
                        {
                            return -2147483638;
                        }
                    else if ( c.ToArgb() == SystemColors.ActiveCaption.ToArgb())
                        {
                            return -2147483646;
                        }
                    else if ( c.ToArgb() == SystemColors.ActiveCaptionText.ToArgb())
                        {
                            return -2147483639;
                        }
                    else if ( c.ToArgb() == SystemColors.AppWorkspace.ToArgb())
                        {
                            return -2147483636;
                        }
                    else if ( c.ToArgb() == SystemColors.Control.ToArgb())
                        {
                            return -2147483633;
                        }
                    else if ( c.ToArgb() == SystemColors.ControlDark.ToArgb())
                        {
                            return -2147483632;
                        }
                    else if ( c.ToArgb() == SystemColors.ControlDarkDark.ToArgb())
                        {
                            return -2147483627;
                        }
                    else if ( c.ToArgb() == SystemColors.ControlLight.ToArgb())
                        {
                            return -2147483626;
                        }
                    else if ( c.ToArgb() == SystemColors.ControlLightLight.ToArgb())
                        {
                            return -2147483628;
                        }
                    else if ( c.ToArgb() == SystemColors.ControlText.ToArgb())
                        {
                            return -2147483630;
                        }
                    else if ( c.ToArgb() == SystemColors.Desktop.ToArgb())
                        {
                            return -2147483647;
                        }
                    else if ( c.ToArgb() == SystemColors.GrayText.ToArgb())
                        {
                            return -2147483631;
                        }
                    else if ( c.ToArgb() == SystemColors.Highlight.ToArgb())
                        {
                            return -2147483635;
                        }
                    else if ( c.ToArgb() == SystemColors.HighlightText.ToArgb())
                        {
                            return -2147483634;
                        }
                    else if ( c.ToArgb() == SystemColors.HotTrack.ToArgb())
                        {
                            return -2147483635;
                        }
                    else if ( c.ToArgb() == SystemColors.InactiveBorder.ToArgb())
                        {
                            return -2147483637;
                        }
                    else if ( c.ToArgb() == SystemColors.InactiveCaption.ToArgb())
                        {
                            return -2147483645;
                        }
                    else if ( c.ToArgb() == SystemColors.InactiveCaptionText.ToArgb())
                        {
                            return -2147483629;
                        }
                    else if ( c.ToArgb() == SystemColors.Info.ToArgb())
                        {
                            return -2147483624;
                        }
                    else if ( c.ToArgb() == SystemColors.InfoText.ToArgb())
                        {
                            return -2147483625;
                        }
                    else if ( c.ToArgb() == SystemColors.Menu.ToArgb())
                        {
                            return -2147483644;
                        }
                    else if ( c.ToArgb() == SystemColors.MenuText.ToArgb())
                        {
                            return -2147483641;
                        }
                    else if ( c.ToArgb() == SystemColors.ScrollBar.ToArgb())
                        {
                            return -2147483648;
                        }
                    else if ( c.ToArgb() == SystemColors.Window.ToArgb())
                        {
                            return -2147483643;
                        }
                    else if ( c.ToArgb() == SystemColors.WindowFrame.ToArgb())
                        {
                            return -2147483642;
                        }
                    else if ( c.ToArgb() == SystemColors.WindowText.ToArgb())
                        {
                            return -2147483640;
                        }
            }
            return ColorTranslator.ToWin32(c);
        }
        public static int ToWin32(Color c)
        {
            return ((c.R | (c.G << Win32GreenShift)) | (c.B << Win32BlueShift));
        }
        // Fields
        //private static Hashtable htmlSysColorTable;
        private const int Win32BlueShift = 0x10;
        private const int Win32GreenShift = 8;
        private const int Win32RedShift = 0;
    }
 
    [StructLayout(LayoutKind.Sequential)]
    public sealed class tagSIZEL
    {
        public int cx;
        public int cy;
    }
 
    [StructLayout(LayoutKind.Sequential)]
    public sealed class POINTL
    {
        public int x;
        public int y;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
        public RECT(Rectangle r)
        {
            this.left = r.X;
            this.top = r.Y;
            this.right = r.Right;
            this.bottom = r.Bottom;
        }
        public RECT(int left, int top, int right, int bottom)
        {
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
        }
        public static RECT FromXYWH(int x, int y, int width, int height)
        {
            return new RECT(x, y, x + width, y + height);
        }

        public Size Size
        {
            get
            {
                return new Size(this.right - this.left, this.bottom - this.top);
            }
        }
    }
 
    [StructLayout(LayoutKind.Sequential)]
    public struct MSG
    {
        public IntPtr hwnd;
        public int message;
        public IntPtr wParam;
        public IntPtr lParam;
        public int time;
        public int pt_x;
        public int pt_y;
    }

    [StructLayout(LayoutKind.Sequential)]
    public sealed class tagPOINTF
    {
        [MarshalAs(UnmanagedType.R4)]
        public float x;
        [MarshalAs(UnmanagedType.R4)]
        public float y;
    }

    [StructLayout(LayoutKind.Sequential)]
    public sealed class tagOIFI
    {
        [MarshalAs(UnmanagedType.U4)]
        public int cb;
        public bool fMDIApp;
        public IntPtr hwndFrame;
        public IntPtr hAccel;
        [MarshalAs(UnmanagedType.U4)]
        public int cAccelEntries;
    }

    [StructLayout(LayoutKind.Sequential)]
    public sealed class tagSIZE
    {
        public int cx;
        public int cy;
    }

    [StructLayout(LayoutKind.Sequential)]
    public sealed class tagCONTROLINFO
    {
        [MarshalAs(UnmanagedType.U4)]
        public int cb;
        public IntPtr hAccel;
        [MarshalAs(UnmanagedType.U2)]
        public short cAccel;
        [MarshalAs(UnmanagedType.U4)]
        public int dwFlags;
    }

    [StructLayout(LayoutKind.Sequential)]
    public sealed class tagOLEVERB
    {
        public int lVerb;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string lpszVerbName;
        [MarshalAs(UnmanagedType.U4)]
        public int fuFlags;
        [MarshalAs(UnmanagedType.U4)]
        public int grfAttribs;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct FORMATETC
    {
        [MarshalAs(UnmanagedType.U2)]
        public short cfFormat;
        public IntPtr ptd;
        [MarshalAs(UnmanagedType.U4)]
        public /*DVASPECT*/ int dwAspect;
        public int lindex;
        [MarshalAs(UnmanagedType.U4)]
        public TYMED tymed;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct STGMEDIUM
    {
        public TYMED tymed;
        public IntPtr unionmember;
        [MarshalAs(UnmanagedType.IUnknown)]
        public object pUnkForRelease;
    }

    [Flags]
    public enum TYMED
    {
        // Fields
        TYMED_ENHMF = 0x40,
        TYMED_FILE = 2,
        TYMED_GDI = 0x10,
        TYMED_HGLOBAL = 1,
        TYMED_ISTORAGE = 8,
        TYMED_ISTREAM = 4,
        TYMED_MFPICT = 0x20,
        TYMED_NULL = 0
    }

    [Flags]
    public enum ADVF
    {
        // Fields
        ADVF_DATAONSTOP = 0x40,
        ADVF_NODATA = 1,
        ADVF_ONLYONCE = 4,
        ADVF_PRIMEFIRST = 2,
        ADVFCACHE_FORCEBUILTIN = 0x10,
        ADVFCACHE_NOHANDLER = 8,
        ADVFCACHE_ONSAVE = 0x20
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct STATDATA
    {
        public FORMATETC formatetc;
        public ADVF advf;
        public IAdviseSink advSink;
        public int connection;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public class LOGFONT
    {
        public int lfHeight;
        public int lfWidth;
        public int lfEscapement;
        public int lfOrientation;
        public int lfWeight;
        public byte lfItalic;
        public byte lfUnderline;
        public byte lfStrikeOut;
        public byte lfCharSet;
        public byte lfOutPrecision;
        public byte lfClipPrecision;
        public byte lfQuality;
        public byte lfPitchAndFamily;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
        public string lfFaceName;
        public LOGFONT()
        {
        }
        public LOGFONT(LOGFONT lf)
        {
            this.lfHeight = lf.lfHeight;
            this.lfWidth = lf.lfWidth;
            this.lfEscapement = lf.lfEscapement;
            this.lfOrientation = lf.lfOrientation;
            this.lfWeight = lf.lfWeight;
            this.lfItalic = lf.lfItalic;
            this.lfUnderline = lf.lfUnderline;
            this.lfStrikeOut = lf.lfStrikeOut;
            this.lfCharSet = lf.lfCharSet;
            this.lfOutPrecision = lf.lfOutPrecision;
            this.lfClipPrecision = lf.lfClipPrecision;
            this.lfQuality = lf.lfQuality;
            this.lfPitchAndFamily = lf.lfPitchAndFamily;
            this.lfFaceName = lf.lfFaceName;
        }

    }


}
